<html>
<head><meta charset="utf-8"><title>null pointer address alignment · t-lang/wg-unsafe-code-guidelines · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/index.html">t-lang/wg-unsafe-code-guidelines</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/null.20pointer.20address.20alignment.html">null pointer address alignment</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="148177985"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/null%20pointer%20address%20alignment/near/148177985" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/null.20pointer.20address.20alignment.html#148177985">(Nov 22 2018 at 14:21)</a>:</h4>
<p>Question: is the address of null pointer (<code>ptr::null()</code>/<code>ptr::null_mut()</code>) always a multiple of the alignment of <code>T</code> ?</p>



<a name="148178049"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/null%20pointer%20address%20alignment/near/148178049" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/null.20pointer.20address.20alignment.html#148178049">(Nov 22 2018 at 14:22)</a>:</h4>
<p>For <code>0x0</code> and <code>0x1</code> this is true, but it wouldn't be true for <code>0xfffff</code>.</p>



<a name="148178071"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/null%20pointer%20address%20alignment/near/148178071" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hanna Kruppe <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/null.20pointer.20address.20alignment.html#148178071">(Nov 22 2018 at 14:23)</a>:</h4>
<p>it is if that address is 0; if not then clearly it cannot be for arbitrary T unless we restrict the address in a ridiculous manner like requiring it's either 0 or the middle of the address space</p>



<a name="148178078"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/null%20pointer%20address%20alignment/near/148178078" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hanna Kruppe <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/null.20pointer.20address.20alignment.html#148178078">(Nov 22 2018 at 14:23)</a>:</h4>
<p>0x1 is not aligned for any T except for with align 1</p>



<a name="148178168"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/null%20pointer%20address%20alignment/near/148178168" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hanna Kruppe <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/null.20pointer.20address.20alignment.html#148178168">(Nov 22 2018 at 14:25)</a>:</h4>
<p>why does it matter anyway? usually alignment matters because you want it to be a reference (but references aren't nullable) or because you want to stash something in the lower order bits (which you can do regardless of the address of the null pointer if you don't need to tag the null pointer, which typically you don't)</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>